package org.siam.impl.data.formatting {

	import mx.formatters.Formatter;
	
	import org.siam.core.data.formatting.IFormatter;
	import org.siam.impl.data.formatting.formatters.DefaultFormatter;
	
	internal class FormatterWrapper implements IFormatter {
		
		private static const FORMAT_STRING:String = "formatString"; 
		
		private var _original:Formatter;
		private var _formatString:String;
		private var _supportsFormatString:Boolean;
		
		public function FormatterWrapper(original:Formatter) {
			_original = original;
			_supportsFormatString = original.hasOwnProperty(FORMAT_STRING);
		}
		
		public function get supportsFormatString():Boolean {
			return supportsFormatString;
		}
		
		//*** IFormattingManager ********************************************
		
		public function get formatString():String {
			return _formatString;
		}
		
		public function set formatString(value:String):void {
			if (_supportsFormatString && value != _formatString) {
				_original[FORMAT_STRING] = value;
				_formatString = value;
			}
		}
		
		public function format(value:Object):String {
			var formattedString:String = _original.format(value);
			if (_original.error) {
				formattedString = DefaultFormatter.INSTANCE.format(value);
			}
			return formattedString;
		}	
	}
}